package com.baojee.index.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 业务返回接口
 */
public class ResponseResult implements Serializable {

    /**
     * 业务状态,ResultCode success/fail
     */
    public static final String ResultCode = "resultCode";

    /**
     * 业务错误码
     */
    public static final String ErrorCode = "errorCode";

    /**
     * 业务错误码说明
     */
    public static final String ErrorCodeMsg = "errorCodeMsg";

    /**
     * 成功
     */
    public static final String success = "success";

    /**
     * 失败
     */
    public static final String fail = "fail";

    /**
     * 正确信息
     */
    public static final String ok = "ok";

    /**
     * 返回状态, success/fail
     */
    private String returnResult = "";

    /**
     * 返回编码
     */
    private String returnCode = "";

    /**
     * 返回信息,错误描述
     */
    private String returnMsg = "";

    /**
     * 返回数据
     */
    private Map<String, Object> response = new HashMap<>();


    /**
     * 业务成功返回
     *
     * @return ResponseResult
     */
    public static ResponseResult success() {
        ResponseResult result = new ResponseResult();
        result.setReturnResult(success);
        result.setReturnCode("");
        result.setReturnMsg(ok);
        result.response.put(ResultCode, success);
        result.response.put(ErrorCode, "");
        result.response.put(ErrorCodeMsg, "");
        return result;
    }

    /**
     * 业务错误返回
     *
     * @param errorCode    错误编码
     * @param errorCodeMsg 错误信息
     * @return ResponseResult
     */
    public static ResponseResult fail(final String errorCode, final String errorCodeMsg) {
        ResponseResult result = new ResponseResult();
        result.setReturnResult(success);
        result.setReturnCode("");
        result.setReturnMsg(ok);
        result.response.put(ResultCode, fail);
        result.response.put(ErrorCode, errorCode);
        result.response.put(ErrorCodeMsg, errorCodeMsg);
        return result;
    }

    /**
     * 请求错误,参数出错,或者校验出错,出异常等
     *
     * @param returnMsg 错误信息
     * @return ResponseResult
     */
    public static ResponseResult error(final String returnCode, final String returnMsg) {
        ResponseResult result = new ResponseResult();
        result.setReturnResult(fail);
        result.setReturnCode(returnCode);
        result.setReturnMsg(returnMsg);
        return result;
    }

    public ResponseResult put(String key, Object value) {
        response.put(key, value);
        return this;
    }

    public String getReturnResult() {
        return returnResult;
    }

    public void setReturnResult(String returnResult) {
        this.returnResult = returnResult;
    }

    public String getReturnMsg() {
        return returnMsg;
    }

    public void setReturnMsg(String returnMsg) {
        this.returnMsg = returnMsg;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    public Map<String, Object> getResponse() {
        return response;
    }

    public void setResponse(Map<String, Object> response) {
        this.response = response;
    }
}
